package com.cmwa.solution.business.investmentResearch.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cmwa.solution.base.controller.BaseController;
import com.cmwa.solution.base.entity.Page;
import com.cmwa.solution.base.entity.Result;
import com.cmwa.solution.base.entity.SearchParam;
import com.cmwa.solution.business.investmentResearch.entity.InvestmentResearchType;
import com.cmwa.solution.business.investmentResearch.entity.InvestmentResearchVo;
import com.cmwa.solution.business.investmentResearch.service.InvestmentResearchService;
import com.cmwa.solution.sys.attach.entity.AttachVo;
import com.cmwa.solution.sys.attach.service.AttachService;
import com.google.common.base.Joiner;

/**
 * 投资研究控制器
 * 
 * @author ex-sujh
 * @date 2019-02-14 09:42:22
 */
@Controller
@RequestMapping("/business/investmentResearch")
public class InvestmentResearchController extends BaseController {

	private final static String JSP_PATH = "business/investmentResearch/";
	@Resource
	private InvestmentResearchService investmentResearchService;
	@Resource
	private AttachService attachService;
	
	
	@RequestMapping("/home")
	public String home(ModelMap model) {
		model.addAttribute("researchType", "1");
		return JSP_PATH + "home";
	}
	
	@RequestMapping("/deleteInvestmentResearch")
	@ResponseBody
	public Result<Object> deleteInvestmentResearch(@RequestParam("id") String id) {
		Result<Object> result = new Result<Object>();
		investmentResearchService.logicDeleteInvestmentResearchById(id);
		return result;
	}
	
	@RequestMapping("/investmentResearchListPage")
	@ResponseBody
	public Page<InvestmentResearchVo> investmentResearchListPage(SearchParam param) {
		Page<InvestmentResearchVo> list = investmentResearchService.investmentResearchListPage(param);
		return list;
	}
	
	/**
	 * 跳转到新增页面
	 */
	@RequestMapping("/investmentResearchAddView")
	public String investmentResearchAddView(@RequestParam("researchType") String researchType, ModelMap model) {
		model.addAttribute("researchType", researchType);
		model.addAttribute("title", InvestmentResearchType.get(researchType));
		return JSP_PATH + "investmentResearchAdd";
	}
	
	/**
	 * 新增保存
	 * @param investmentResearchVo
	 * @param attIds
	 * @return
	 */
	@RequestMapping("/saveInvestmentResearch")
	public String saveInvestmentResearch(InvestmentResearchVo investmentResearchVo, @RequestParam("attIds")String attIds) {
		investmentResearchService.saveInvestmentResearch(investmentResearchVo, attIds);
		return redirectSuccess();
	}
	
	/**
	 * 跳转到详情页面
	 */
	@RequestMapping("/investmentResearchDetailView")
	public String investmentResearchDetailView(@RequestParam("id") String id, ModelMap model) {
		InvestmentResearchVo investmentResearchVo = investmentResearchService.getInvestmentResearchById(id);
		model.addAttribute("investmentResearchVo", investmentResearchVo);
		List<AttachVo> attachVos = attachService.queryAttachByRelIdAndType(id, "INVESTMENT_RESEARCH");
		model.addAttribute("attachVos", attachVos);
		
		return JSP_PATH + "investmentResearchDetail";
	}
	
	/**
	 * 打包下载附件-通用
	 * @param id
	 * @throws IOException 
	 */
	@RequestMapping("/downloadInvestmentResearchAttr")
	public void downloadInvestmentResearchAttr(@RequestParam("id") String id, HttpServletResponse response, HttpServletRequest request) throws IOException {
		InvestmentResearchVo investmentResearchVo = investmentResearchService.getInvestmentResearchById(id);
		List<AttachVo> attachVos = attachService.queryAttachByRelIdAndType(id, "INVESTMENT_RESEARCH");
		if (investmentResearchVo != null && CollectionUtils.isNotEmpty(attachVos)) {
			List<String> attIds = new ArrayList<>();
			for (AttachVo attach : attachVos) {
				attIds.add(attach.getAttId());
			}
			//如无特殊需求，调用公共批量下载方法
			String fileName = investmentResearchVo.getResearchTitle();
			attachService.downloadAttachZip(Joiner.on(",").join(attIds), fileName, request, response);
		}
	}
}
